package com.distributeredis.redis_springboot.springeasyexcel.writetest.write;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.distributeredis.redis_springboot.springeasyexcel.vo.*;
import com.distributeredis.redis_springboot.springeasyexcel.vo.Excel样式.DemoStyleData;
import com.distributeredis.redis_springboot.springeasyexcel.vo.Excel样式.WidthAndHeightData;
import com.distributeredis.redis_springboot.springeasyexcel.vo.合并单元格模板.DemoMergeData;
import com.distributeredis.redis_springboot.springeasyexcel.vo.复杂头.ComplexHeadUser;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class WriteTestPlus {
    String PATH = "H:\\LUA\\redis_springboot\\src\\main\\java\\com\\distributeredis\\redis_springboot\\springeasyexcel\\writetest\\";

    /**
     * 复杂头编写
     */
    @Test
    public void test() {
        String fileName = "user5.xlsx";
        List<ComplexHeadUser> users = Arrays.asList(
                new ComplexHeadUser(110, "李雷", new Date()),
                new ComplexHeadUser(111, "张三", new Date()),
                new ComplexHeadUser(112, "李四", new Date()),
                new ComplexHeadUser(113, "王五", new Date()),
                new ComplexHeadUser(114, "赵六", new Date())
        );

        EasyExcel.write(PATH+fileName, ComplexHeadUser.class)
                .sheet("用户信息")
                .doWrite(users);
    }

    /**
     * 输入重复写到同一个excel表格的同一个sheet标签中
     */
    @Test
    public void test1() {
        String fileName = "user6.xlsx";
        List<User> users = Arrays.asList(
                new User(1001, "李雷", "男", 100.0, new Date()),
                new User(1002, "张三", "男", 110.0, new Date()),
                new User(1003, "李四", "女", 120.0, new Date()),
                new User(1004, "王五", "女", 130.0, new Date()));
        // excel表格中写入数据  重复写
        // 创建excelWriter
        ExcelWriter excelWriter = EasyExcel.write(PATH+fileName, User.class).build();
        // 创建writeSheet
        WriteSheet writeSheet = EasyExcel.writerSheet("用户信息6").build();
        for (int i = 0; i < 5; i++) {
            excelWriter.write(users,writeSheet);
        }
        // 关闭
        excelWriter.finish();
    }

    /**
     * 输入重复写到同一个excel表格的不同sheet标签中
     */
    @Test
    public void test2() {
        String fileName = "user7.xlsx";
        List<User> users = Arrays.asList(
                new User(1001, "李雷", "男", 100.0, new Date()),
                new User(1002, "张三", "男", 110.0, new Date()),
                new User(1003, "李四", "女", 120.0, new Date()),
                new User(1004, "王五", "女", 130.0, new Date()));
        // excel表格中写入数据  重复写
        // 创建excelWriter
        ExcelWriter excelWriter = EasyExcel.write(PATH+fileName, User.class).build();
        // 创建writeSheet
        for (int i = 0; i < 5; i++) {
            WriteSheet writeSheet = EasyExcel.writerSheet("用户信息"+i).build();
            excelWriter.write(users,writeSheet);
        }
        // 关闭
        excelWriter.finish();
    }
    /**
     * 将图片写入到Excel
     */
    @Test
    public void test3() throws IOException {
        String fileName = "user8.xlsx";
        // 构建数据
        List<ImageData> list = new ArrayList<>();
        ImageData imageData = new ImageData();
        String imagePath = "H:\\LUA\\redis_springboot\\src\\main\\resources\\templates\\1.jpg";
        imageData.setFile(new File(imagePath));
        imageData.setInputStream(new FileInputStream(new File(imagePath)));
        imageData.setString(imagePath);
        imageData.setUrl(new URL("http://tse2-mm.cn.bing.net/th/id/OIP-C.duz6S7Fvygrqd6Yj_DcXAQHaF7?rs=1&pid=ImgDetMain"));
        byte[] b = new byte[(int) new File(imagePath).length()];
        FileInputStream in = new FileInputStream(imagePath);
        in.read(b,0,(int) new File(imagePath).length());
        imageData.setByteArray(b);
        list.add(imageData);
        // 写数据
        EasyExcel.write(PATH+fileName, ImageData.class)
                .sheet("img").doWrite(list);
    }

    /**
     * 设置列宽，高度
     * @throws IOException
     */
    @Test
    public void test4() throws IOException {
        String fileName = "user9.xlsx";
        List<WidthAndHeightData> list = new ArrayList<>();
        list.add(new WidthAndHeightData("11",new Date(),11.9191));
        // 写数据
        EasyExcel.write(PATH+fileName, WidthAndHeightData.class)
                .sheet("img").doWrite(list);
    }

    /**
     * 设置Excel样式
     * @throws IOException
     */
    @Test
    public void test5() throws IOException {
        String fileName = "user10.xlsx";
        List<DemoStyleData> list = new ArrayList<>();
        list.add(new DemoStyleData("11",new Date(),11.9191));
        list.add(new DemoStyleData("11",new Date(),11.9191));
        list.add(new DemoStyleData("11",new Date(),11.9191));
        // 写数据
        EasyExcel.write(PATH+fileName, DemoStyleData.class)
                .sheet("img").doWrite(list);
    }

    /**
     * 合并单元格
     * @throws IOException
     */
    @Test
    public void test6() throws IOException {
        String fileName = "user11.xlsx";
        List<DemoMergeData> list = new ArrayList<>();
        list.add(new DemoMergeData("11",new Date(),11.9191));
        list.add(new DemoMergeData("12",new Date(),11.9191));
        list.add(new DemoMergeData("13",new Date(),11.9191));
        list.add(new DemoMergeData("14",new Date(),11.9191));
        list.add(new DemoMergeData("15",new Date(),11.9191));
        list.add(new DemoMergeData("16",new Date(),11.9191));
        list.add(new DemoMergeData("17",new Date(),11.9191));
        list.add(new DemoMergeData("18",new Date(),11.9191));
        // 写数据
        EasyExcel.write(PATH+fileName, DemoMergeData.class)
                .sheet("img").doWrite(list);
    }
}
